package com.fa.utils;

import java.util.HashMap;
import java.util.Map;

public class FieldTypeMapper {

    // 数据库字段类型到 Java 字段类型的映射
    private static final Map<String, String> typeMapping = new HashMap<>();

    static {
        // 数值类型映射
        typeMapping.put("TINYINT", "Byte");
        typeMapping.put("SMALLINT", "Short");
        typeMapping.put("MEDIUMINT", "Integer");
        typeMapping.put("INT", "Integer");
        typeMapping.put("INTEGER", "Integer");
        typeMapping.put("BIGINT", "Long");
        typeMapping.put("FLOAT", "Float");
        typeMapping.put("DOUBLE", "Double");
        typeMapping.put("DECIMAL", "BigDecimal");
        typeMapping.put("NUMERIC", "BigDecimal");

        // 字符串类型映射
        typeMapping.put("CHAR", "String");
        typeMapping.put("VARCHAR", "String");
        typeMapping.put("TEXT", "String");
        typeMapping.put("TINYTEXT", "String");
        typeMapping.put("MEDIUMTEXT", "String");
        typeMapping.put("LONGTEXT", "String");
        typeMapping.put("BLOB", "byte[]");
        typeMapping.put("TINYBLOB", "byte[]");
        typeMapping.put("MEDIUMBLOB", "byte[]");
        typeMapping.put("LONGBLOB", "byte[]");

        // 日期和时间类型映射
        typeMapping.put("DATE", "LocalDate");
        typeMapping.put("TIME", "LocalTime");
        typeMapping.put("YEAR", "Integer");
        typeMapping.put("DATETIME", "LocalDateTime");
        typeMapping.put("TIMESTAMP", "LocalDateTime");

        // 布尔类型映射
        typeMapping.put("BIT", "Boolean");
        typeMapping.put("BOOLEAN", "Boolean");
        typeMapping.put("BOOL", "Boolean");

        // 二进制数据类型映射
        typeMapping.put("BINARY", "byte[]");
        typeMapping.put("VARBINARY", "byte[]");

        // JSON 和 XML 类型映射
        typeMapping.put("JSON", "String");
        typeMapping.put("XML", "String");

        // 枚举和集合类型映射
        typeMapping.put("ENUM", "String");
        typeMapping.put("SET", "String");

        // 地理空间数据类型映射 (常见于 MySQL)
        typeMapping.put("GEOMETRY", "Object");
        typeMapping.put("POINT", "Object");
        typeMapping.put("LINESTRING", "Object");
        typeMapping.put("POLYGON", "Object");
        typeMapping.put("MULTIPOINT", "Object");
        typeMapping.put("MULTILINESTRING", "Object");
        typeMapping.put("MULTIPOLYGON", "Object");
        typeMapping.put("GEOMETRYCOLLECTION", "Object");

        // 其他类型映射
        typeMapping.put("UUID", "String"); // PostgreSQL 中常见的类型
    }

    /**
     * 根据数据库字段类型获取对应的 Java 类型
     *
     * @param dbType 数据库字段类型
     * @return 对应的 Java 类型
     */
    public static String mapToJavaType(String dbType) {
        return typeMapping.getOrDefault(dbType.toUpperCase(), "Object");  // 默认返回 Object 类型
    }
}
